package org.PropertyManageMentSystem.Manager;

import org.PropertyManageMentSystem.Data.MysqlConfig;
import org.PropertyManageMentSystem.Data.Schedule;
import org.PropertyManageMentSystem.Data.Task;

import java.sql.*;
import java.util.ArrayList;

public class ScheduleManager {
    private Connection conn;

    public ScheduleManager(MysqlConfig config) throws SQLException, ClassNotFoundException {
        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            //创建连接
            conn = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPassword());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void addSchedule(Long workerid, Long taskid, Long schedulerId) throws SQLException {
        String sql="insert into schedule (workerId, taskId, schedulerId, createTime, state) values(?,?,?,?,?)";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setLong(1, workerid);
        pst.setLong(2, taskid);
        pst.setLong(3, schedulerId);
        Timestamp createTime = new Timestamp(System.currentTimeMillis());
        pst.setTimestamp(4, createTime);
        int stateNum = 0;
        pst.setInt(5, stateNum);
        pst.executeUpdate();
        pst.close();
    }

    public ArrayList<Schedule> getScheduleBySchedulerId(Long schedulerId) throws SQLException {
        String sql="select * from schedule where schedulerId=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setLong(1, schedulerId);
        ResultSet rs = pst.executeQuery();
        ArrayList<Schedule> schedules = new ArrayList<>();
        while (rs.next()) {
            Schedule schedule = new Schedule(rs.getLong("scheduleId"), rs.getLong("workerId"),
                    rs.getLong("taskId"), schedulerId, rs.getTimestamp("createTime"),
                    Schedule.ScheduleState.values()[rs.getInt("state")]);
            schedules.add(schedule);
        }
        if (schedules.isEmpty()) {
            return null;
        }
        else {
            return schedules;
        }
    }
    public ArrayList<Schedule> getScheduleByWorkerId(Long workerId) throws SQLException {
        String sql="select * from schedule where workerId=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setLong(1, workerId);
        ResultSet rs = pst.executeQuery();
        ArrayList<Schedule> schedules = new ArrayList<>();
        while (rs.next()) {
            Schedule schedule = new Schedule(rs.getLong("scheduleId"), workerId,
                    rs.getLong("taskId"), rs.getLong("schedulerId"),
                    rs.getTimestamp("createTime"),
                    Schedule.ScheduleState.values()[rs.getInt("state")]);
            schedules.add(schedule);
        }
        if (schedules.isEmpty()) {
            return null;
        }
        else {
            return schedules;
        }
    }

    public ArrayList<Schedule> getScheduleByTaskId(Long taskId) throws SQLException {
        String sql="select * from schedule where taskId=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setLong(1, taskId);
        ResultSet rs = pst.executeQuery();
        ArrayList<Schedule> schedules = new ArrayList<>();
        while (rs.next()) {
            Schedule schedule = new Schedule(rs.getLong("scheduleId"), rs.getLong("workerId"),
                    taskId, rs.getLong("schedulerId"), rs.getTimestamp("createTime"),
                    Schedule.ScheduleState.values()[rs.getInt("state")]);
            schedules.add(schedule);
        }
        if (schedules.isEmpty()) {
            return null;
        }
        else {
            return schedules;
        }
    }

    public Long getTaskIdByScheduleId(Long scheduleId) throws SQLException {
        String sql="select taskId from schedule where scheduleId=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setLong(1, scheduleId);
        ResultSet rs = pst.executeQuery();
        rs.next();
        return rs.getLong("taskId");
    }
    private void changeTaskStateByTaskId(Long taskId, Task.TaskStates state) throws SQLException {
        String sql = "update task set state=? where taskId=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setLong(2, taskId);
        pst.setInt(1, state.ordinal());
        pst.executeUpdate();
    }
    public void acceptSchedule(Long scheduleId) throws SQLException {
        Schedule.ScheduleState state = Schedule.ScheduleState.accept;
        String sql="update schedule set state=? where scheduleId=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1, state.ordinal());
        pst.setLong(2, scheduleId);
        pst.executeUpdate();
        Long taskId = getTaskIdByScheduleId(scheduleId);
        changeTaskStateByTaskId(taskId, Task.TaskStates.processing);
    }
    public void reSchedule(Long scheduleId) throws SQLException {
        Schedule.ScheduleState state = Schedule.ScheduleState.reSchedule;
        String sql="update schedule set state=? where scheduleId=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1, state.ordinal());
        pst.setLong(2, scheduleId);
        pst.executeUpdate();
        Long taskId = getTaskIdByScheduleId(scheduleId);
        changeTaskStateByTaskId(taskId, Task.TaskStates.unscheduled);
    }
    public void finishSchedule(Long scheduleId) throws SQLException {
        Schedule.ScheduleState state = Schedule.ScheduleState.finish;
        String sql="update schedule set state=? where scheduleId=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1, state.ordinal());
        pst.setLong(2, scheduleId);
        pst.executeUpdate();
        Long taskId = getTaskIdByScheduleId(scheduleId);
        changeTaskStateByTaskId(taskId, Task.TaskStates.accomplished);
    }

}
